package aiball


class UserAccount {

	transient springSecurityService

	String username
	String email;
	String password
	Team team;
	Date lastActivity;
	boolean enabled
	boolean accountExpired
	boolean accountLocked
	boolean passwordExpired

	static constraints = {
		username blank: false, unique: true
		password password: true, blank: false
		email email: true, blank: false, unique: true
		lastActivity blank: true, display: false
		team blank: true, display: false
		enabled display: false
		accountExpired display: false
		accountLocked display: false
		passwordExpired display: false
	}

	static mapping = {
		password column: '`password`'
	}

	Set<UserRoles> getAuthorities() {
		UserAccountUserRoles.findAllByUserAccount(this).collect { it.userRoles } as Set
	}

	def beforeInsert() {
		encodePassword()
	}
	

	def beforeUpdate() {
		if (isDirty('password')) {
			encodePassword()
		}
	}

	protected void encodePassword() {
		password = springSecurityService.encodePassword(password)
	}
}
